package com.shizhanzhe.zxbapp.customtask;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.CheckBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;


import butterknife.BindView;
import butterknife.ButterKnife;

public class TaskidGetChecksActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    private int page = 1;
    private CheckAdapter mAdapter;
    private int taskid = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taskid_get_checks);
        ButterKnife.bind(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorAccent).init();
        taskid = getIntent().getIntExtra("taskid",0);
        initData();
        setListener();
    }
    private void initData(){
        topTitle.setText("提交列表");
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CheckAdapter(this);
        recycler.setAdapter(mAdapter);
        screen(page);
    }

    private void setListener(){
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                screen(page);
            }
        });
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                screen(page);
            }
        });
        mAdapter.setScreenItemListener(new CheckAdapter.ScreenItemListener() {
            @Override
            public void onItemClick(CheckBean.DataBeanX.DataBean item) {
                startActivity(new Intent(TaskidGetChecksActivity.this,PictureUploadDetailsActivity.class).putExtra("isshow",0).putExtra("checkid",item.getCheckid()));
            }
        });
    }
    private void screen(final int page){
        OkHttpDownloadJsonUtil.downloadJson(TaskidGetChecksActivity.this, Path.subCheck + "?taskid=" + taskid + "&page=" + page, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"0".equals(json)){
                    Gson gson = new Gson();
                    CheckBean screenBean = gson.fromJson(json, CheckBean.class);
                    if (screenBean.getData().getData().size()>0){
                        mAdapter.updateData(page,screenBean.getData().getData());
                    } else {
                        if (page == 1){
                            Toast.makeText(TaskidGetChecksActivity.this, "暂无任务,请稍候再来", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(TaskidGetChecksActivity.this, "没有更多任务了", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else {
                    Toast.makeText(TaskidGetChecksActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
